use ggez::{Context, GameResult, GameError, graphics};
use rand::Rng;

pub struct State{
    random_counts: Vec<f32>,
}

impl State{
    pub fn new() -> State {
        State{random_counts : vec![0.0; 20]}
    }
}

impl ggez::event::EventHandler<GameError> for State {
    fn update(&mut self, _ctx: &mut Context) -> GameResult {
        let mut rng = rand::thread_rng();
        self.random_counts[rng.gen_range(0..20)] += 1.0;
        Ok(())
    }
    fn draw(&mut self, ctx: &mut Context) -> GameResult {
        let mut canvas = graphics::Canvas::from_frame(ctx, graphics::Color::WHITE);

        for i in 0..20 { 
            let rectangle = graphics::Mesh::new_rectangle(
                ctx,
                graphics::DrawMode::stroke(3.0),
                graphics::Rect::new((i as f32)*20.0, 500.0, 20.0, -self.random_counts[i]),
                graphics::Color::BLACK,
            )?;
            canvas.draw(&rectangle, graphics::DrawParam::default());
        }

        canvas.finish(ctx)?;
        Ok(())
    }
}